package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.adapter.IndexAuctionAdapter;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.bean.GoodsInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.GridSpaceItemDecoration;
import com.hema.auction.widget.view.MyLoadMoreView;
import com.hema.auction.widget.view.RefreshRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 十元专区
 * author by john
 */

public class TenZoneActivity extends BaseActivity {


    @BindView(R.id.refresh_recycler_view)
    RefreshRecyclerView refreshRecyclerView;
    private IndexAuctionAdapter indexAuctionAdapter;
    private List<GoodsInfo> goodsInfos = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ten_zone);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        indexAuctionAdapter = new IndexAuctionAdapter(goodsInfos);
        indexAuctionAdapter.setLoadMoreView(new MyLoadMoreView());
        indexAuctionAdapter.setTenZone(true);
        indexAuctionAdapter.openLoadAnimation();
        indexAuctionAdapter.setEnableLoadMore(true);
        refreshRecyclerView.setGridLayoutManager(2);
        refreshRecyclerView.setOnRefreshListener(this);
        indexAuctionAdapter.setOnLoadMoreListener(this, refreshRecyclerView.getRecyclerView());
        refreshRecyclerView.setRefresh(true);
        refreshRecyclerView.setAdapter(indexAuctionAdapter);
        GridSpaceItemDecoration itemDecoration = new GridSpaceItemDecoration(this, 1);
        itemDecoration.setHasHeader(false);
        refreshRecyclerView.addItemDecoration(itemDecoration);
        indexAuctionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.EXTRA_GOOD_INFO, indexAuctionAdapter.getItem(position));
                intentTo(GoodsDetailsActivity.class, bundle);
            }
        });
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        pageIndex = 1;
        HttpManager.getInstance(this).getGoodsByCategory("1", pageIndex, pageSize, this);
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        pageIndex++;
        HttpManager.getInstance(this).getGoodsByCategory("1", pageIndex, pageSize, this);
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_GOODS_BY_CATEGORY:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<GoodsInfo> goodsInfos = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<GoodsInfo>>() {
                        }.getType());
                        if (refreshRecyclerView.isRefreshing()) {
                            indexAuctionAdapter.setNewData(goodsInfos);
                            if (goodsInfos.size() < pageSize) {
                                indexAuctionAdapter.setEnableLoadMore(false);
                            } else {
                                indexAuctionAdapter.setEnableLoadMore(true);
                            }
                        } else {
                            indexAuctionAdapter.addData(goodsInfos);
                            indexAuctionAdapter.loadMoreComplete();
                            if (goodsInfos.size() < pageSize) {
                                indexAuctionAdapter.loadMoreEnd();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        refreshRecyclerView.setRefresh(false);
    }
}
